package com.kayak.tg.client.schedule.controller;

import com.baidu.unbiz.fluentvalidator.annotation.FluentValid;
import com.kayak.tg.client.schedule.bean.TaskBean;
import com.kayak.tg.client.schedule.service.ITaskBeanService;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName TestController
 * @Dsecription TODO
 * @Auth tinxi
 * @DATAE 2019/3/25 11:03
 */
@RestController
public class TestController {

	@Autowired
	private ITaskBeanService taskBeanService;

	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;

	@RequestMapping(value = "/get111", method = {RequestMethod.GET})
	public String startDailyTask() {
		try {
//            taskService.startDailyTask();
			TaskBean taskBean = taskBeanService.get(2L);
			System.out.println(taskBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 增加任务
	 * @return
	 */
	@RequestMapping(value = "/addJob", method = {RequestMethod.GET})
	public String addJob(String jobName,String cron,String jobType) {
		// if (StringUtils.isEmpty(jobName)) {
		// 	throw new RuntimeException("任务参数不能为空!");
		// }
		// if (StringUtils.isEmpty(cron)) {
		// 	cron = "0/3 * * * * ? ";
		// }
		// Class clazz = MyJob.class;
		// if ("1".equals(jobType)) {
		// 	clazz = MyJob1.class;
		// }
		// QuartzManager.addJob(jobName, clazz, cron, "run");
		return "new job added!";
	}

	/**
	 * 获取任务清单
	 * @return
	 */
	@RequestMapping(value = "/listJob", method = {RequestMethod.GET})
	public String listJob() {
		// List<MyJob> plannedJobList = QuartzManager.getPlannedJobList();
		// if (plannedJobList != null && plannedJobList.size() > 0) {
		// 	for (MyJob myJob : plannedJobList) {
		// 		String jobId = myJob.getJobId();
		// 		System.out.println(jobId);
		// 	}
		// }
		return "new job added!";
	}

	@RequestMapping(value = "/startJob", method = {RequestMethod.GET})
	public String startJob() throws SchedulerException {
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		boolean b = scheduler.checkExists(JobKey.jobKey("任务1", "DEFAULT_JOB_GROUP_NAME"));
		if (b) scheduler.start();
		return "start Job!";
	}

	@RequestMapping(value = "/stopJob", method = {RequestMethod.GET})
	public String stopJob() throws SchedulerException {
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		boolean b = scheduler.checkExists(JobKey.jobKey("任务1", "DEFAULT_JOB_GROUP_NAME"));
		if (b) scheduler.shutdown();
		return "shutdown job!";
	}

	@RequestMapping(value = "/test", method = {RequestMethod.GET})
	public String test() {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		return "success";
	}

	/**
	 * 新增任务
	 * @param taskBean
	 * @return
	 */
	@PostMapping("/addTask")
	public Object addTask(@RequestBody @FluentValid TaskBean taskBean) {
		TaskBean tb = new TaskBean();
		//这里需要先添加团队信息,再添加团队成员信息,两表的操作
		tb.setJobName(taskBean.getJobName());
		tb.setDescription(taskBean.getDescription());
		tb.setCronExpression(taskBean.getCronExpression());
		taskBeanService.addTaskBean(tb);
		return tb;
	}


}
